home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: Array of pointers to a tree structure ?
- Date: 11 Jan 1996 16:40:35 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4d3ei3$9fl@news.iag.net>
- References: <4d067t$9lc@hermes.fundp.ac.be>
- NNTP-Posting-Host: pm3-orl4.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4d067t$9lc@hermes.fundp.ac.be>, fmelo@biq.fundp.ac.be says...
- >
- >Hi Colleagues, I have been learning C language just from one month ago
- >and I don't know too much about this language. I have a problem with the
- >assignment of pointers in an array of pointers with a specifical defined
- >structure. For example:
- >
- >
- >/* BOXES STRUCTURE FOR THE CONSTRUCTION OF A TREE */
- >
- >struct boxes {
- > int number;
- > struct boxes *pointer_1;
- > struct boxes *pointer_2;
- > struct boxes *pointer_3;
- > }
- >
- >/* ARRAY OF POINTERS TO BOXES STRUCTURE FOR MANAGE */
- >/* SOME BRANCHES OF THE TREE */
- >
- >struct boxes array_pointers [15];
- >
- >
- >Supose that I have the array of pointers already defined, with every
- >pointer pointing to a boxes structure and every boxes structure
- >contains only the integer number defined. In this moment I would like
- >to define the three pointers in every box in the way of these pointers
- >point to another element in the array. I have tried statements like
- >this with out success:
- >
- >
- >(*(array_pointers + 1)).pointer_1 = (array_pointers + 0);
- >
- >or
- >
- >(array_pointers[1]).pointer_1 = &array_pointers[0];
- >
- >
- >Somebody know how can I define a pointer inside the box in the way that
- >it points to a box in the array ???, In advance, thank you very much,
- >best wishes,
-
- Here are some variations. One of them should allow you to accomplish your
- objective. I prefer index notation to dereference notation, but feel free
- to use whichever you happen to like. The compiler will normally generate
- the same code for both.
-
- #include <stdio.h>
-
- struct boxes {
- int number;
- struct boxes *pointer_1;
- struct boxes *pointer_2;
- struct boxes *pointer_3;
- };
-
- int main()
- {
- int i;
- struct boxes array_structs[5]; /* array of struct boxes */
- struct boxes *array_pointers[5]; /* array of pointers to struct boxes */
-
- array_pointers[0] = array_structs;
- array_pointers[1] = array_structs+1;
- array_pointers[2] = array_structs+2;
- array_pointers[3] = array_structs+3;
- array_pointers[4] = &array_structs[4];
-
- array_structs[0].number = 1;
- (*(array_pointers+1))->number = 2;
-
- array_structs[0].pointer_1 = &array_structs[2];
- array_pointers[0]->pointer_2 = &array_structs[3];
- array_pointers[0]->pointer_3 = array_structs + 4;
-
- array_structs[0].pointer_1->number = 3;
- array_pointers[0]->pointer_2->number = 4;
- array_pointers[0]->pointer_3->number = 5;
-
- for( i = 0; i < 5; i++)
- printf( "array_structs%d].number = %d\n", i, array_structs[i]);
-
- return (0);
- }
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-